package com.eshop.service;

import com.eshop.mapper.MenuMapper;
import com.eshop.pojo.Menu;
import com.eshop.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MenuService {

    @Autowired
    private MenuMapper menuMapper;
//    @Autowired
//    private RedisTemplate redisTemplate;

    //根据角色获取菜单列表
    public List<Menu> getMenusWithRole(){
        return menuMapper.getMenusWithRole();
    }

    //根据用户id查询菜单列表
    public List<Menu> getMenusByUserId() {
//        Integer userId = ((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId();
//        ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue();
//        //从Redis获取菜单数据，
//        List<Menu> menus = (List<Menu>) valueOperations.get("menu_" + userId);
//        //如果为空则去数据库获取
//        if (CollectionUtils.isEmpty(menus)) {
//            menus = menuMapper.getMenusByUserId(userId);
//            //将数据设置到Redis中
//            valueOperations.set("menu_"+userId,menus);
//        }
//        return menus;
        return menuMapper.getMenusByUserId(((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId());
    }
}
